package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.Log;
import android.view.View;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.TabEntity;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.FestivalBean;
import com.migu.contacts.databinding.ActivityFestivalWordsBinding;
import com.migu.contacts.databinding.ItemFestivalWordsBinding;
import com.migu.contacts.databinding.ItemFestivalWordsPopFilterBinding;
import com.migu.contacts.databinding.PopWordsFilterBinding;

import java.util.ArrayList;
import java.util.List;

public class FestivalWordsActivity extends BaseActivity<ActivityFestivalWordsBinding> {

    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemFestivalWordsBinding, FestivalBean> adapter = new BaseAdapter<ItemFestivalWordsBinding, FestivalBean>() {
        @OnClick(R.id.confirm)
        public void confirm(FestivalBean bean) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("label", bean.name);
            clipboard.setPrimaryClip(clip);
            finish();
        }
    };

    @Override
    protected void initData() {
        super.initData();
//        showFilter(null);
        ArrayList<CustomTabEntity> list = new ArrayList<>();
        list.add(new TabEntity("全部", 0, 0));
        list.add(new TabEntity("幽默类", 0, 0));
        list.add(new TabEntity("经典类", 0, 0));
        list.add(new TabEntity("文艺类", 0, 0));
        mBinding.tabLayout.setTabData(list);

        mBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                Log.d(TAG, "onTabSelect() called with: position = [" + position + "]");
                select(position);

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        select(0);
    }

    private void select(int position) {
        int festivalId = selected == null ? 0 : selected.id;
        API.queryFestivalGreetings(position, festivalId).watch(mActivity, new HttpCallback<List<FestivalBean>>() {
            @Override
            protected void onSuccess(List<FestivalBean> festivalBeans) {
                if (festivalBeans != null && festivalBeans.size() > 0) {
                    festivalBeans.get(0).isFirst = true;
                }
                adapter.setData(festivalBeans);
            }
        });
    }

    FestivalBean selected = null;

    public void showFilter(View view) {
        CenterPopup bottomPopup = new CenterPopup<PopWordsFilterBinding>(mActivity) {
            FestivalBean checked = null;
            @BindAdapter(R.id.recyclerView)
            BaseAdapter<ItemFestivalWordsPopFilterBinding, FestivalBean> adapter = new BaseAdapter<ItemFestivalWordsPopFilterBinding, FestivalBean>() {

                @Override
                protected void onBindData(ItemFestivalWordsPopFilterBinding binding, FestivalBean bean, int position) {
                    super.onBindData(binding, bean, position);
                    if (bean == checked) {
                        binding.confirm.setSelected(true);
                    } else {
                        binding.confirm.setSelected(false);
                    }
                    binding.confirm.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            checked = bean;
                            notifyDataSetChanged();
                        }
                    });
                }
            };

            @OnClick(R.id.confirm)
            public void confirm() {
                selected = checked;
                select(0);
                dismiss();
            }

            @Override
            protected void initData() {
                super.initData();
                API.queryFestival().watch(mActivity, new HttpCallback<List<FestivalBean>>() {
                    @Override
                    protected void onSuccess(List<FestivalBean> groupBeans) {
                        adapter.setData(groupBeans);
                    }
                });

            }

            @OnClick(R.id.close)
            public void close() {
                dismiss();
            }
        };
        new XPopup.Builder(mActivity).
                asCustom(bottomPopup).
                show();

    }
}